#include "Infrared.h"

Infrared::Infrared()
{
}

Infrared &Infrared::setCallback(INFRARED_CALLBACK_SIGNATURE)
{
    this->callback = callback;
    return *this;
}

void Infrared::setup()
{
    pinMode(INFRARED_ACTION_PIN, INPUT_PULLUP);
}

void Infrared::loop()
{
    // 10ms计数一次
    long now = millis();
    if (now - last > 10)
    {
        last = now;
        // 读取引脚
        int value = digitalRead(INFRARED_ACTION_PIN);
        if (value != pin_value)
        {
            pin_value = value;
            if (pin_value == HIGH)
            {
                callback(INFRARED_ACTION_NOBODY);
            }
            else 
            {
                callback(INFRARED_ACTION_ANYBODY);
            }
        }
    }
}

int Infrared::getValue()
{
    int value = digitalRead(INFRARED_ACTION_PIN);
    if (value == HIGH)
    {
        return INFRARED_ACTION_NOBODY;
    }
    else 
    {
        return INFRARED_ACTION_ANYBODY;
    }
    return -1;
}
